#include "Game.h"
#include "Player.h"
#include "Alien.h"
#include "Scene.h"

using namespace YoghurtGum;

int centerX = Device::GetWidth() / 2;
int centerY = Device::GetHeight() / 2;

struct Stripe
{
	float dirX, dirY;
	float start;
	float length;
};

#define STRIPES_AMOUNT		32
#define STRIPES_START		32
#define STRIPES_END			160
#define STRIPES_MAXLENGTH	16

Stripe stripes[STRIPES_AMOUNT];
float degX[60];
float degY[60];

Player* player;

void Galaxians::Init()
{
	current = new Scene;

	Device::SetColorClear(0);

	Assets::AddSprite(new Sprite(32, 32, "media/Viper_Mark_II.bmp"),	"ship");
	Assets::AddSprite(new Sprite(20, 20, "media/alien.bmp"),			"alien");
	Assets::AddSprite(new Sprite(10, 13, "media/bat_ball.bmp"),			"bullet");

	current->AddObject(new Player());

	for (int i = 0; i < 60; i++)
	{
		degX[i] = CosDeg(i * 6.f);
		degY[i] = SinDeg(i * 6.f);
	}

	for (int i = 0; i < STRIPES_AMOUNT; i++)
	{
		int dir = (int)Random(0, 59);
		stripes[i].dirX = degX[dir];
		stripes[i].dirY = degY[dir];

		stripes[i].start = Random(STRIPES_START, STRIPES_END);
		stripes[i].length = 2.f;
	}

	for (int i = 0; i < 4; ++i)
	{
		current->AddObject(new Alien(8 + (i * 48), 4));
	}
}

void Galaxians::Update()
{
	current->Update();
}

void Galaxians::Render()
{
	for (int i = 0; i < STRIPES_AMOUNT; i++)
	{
		stripes[i].length = 1 + ((stripes[i].start - STRIPES_START) / (STRIPES_END - STRIPES_START)) * STRIPES_MAXLENGTH;

		Draw::Line (
			centerX + (stripes[i].dirX * stripes[i].start),
			centerY + (stripes[i].dirY * stripes[i].start),
			centerX + (stripes[i].dirX * (stripes[i].start + stripes[i].length)),
			centerY + (stripes[i].dirY * (stripes[i].start + stripes[i].length)),
			0xFF00FF
		);

		stripes[i].start += 1.5f;
		if (stripes[i].start > STRIPES_END) 
		{ 
			int dir = (int)Random(0, 59);
			stripes[i].dirX = degX[dir];
			stripes[i].dirY = degY[dir];

			stripes[i].start = STRIPES_START; 
		}
	}

	current->Render();
}